package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISubAccount;
import com.fingard.dsp.bank.reqrespobj.subaccount.ReqQrySubAccount;
import com.fingard.dsp.bank.reqrespobj.subaccount.ReqUpdateSubAccount;
import com.fingard.dsp.bank.reqrespobj.subaccount.RetAtsRecUpdate;
import com.fingard.dsp.bank.reqrespobj.subaccount.RetAtsSubAccountInfo;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class BOC01SubAccount extends BOC01Base implements ISubAccount {
    @Override
    public RetAtsSubAccountInfo querySubAccountInfo(ReqQrySubAccount p_subAccount) throws Exception {
        return null;
    }

    //出口商申请/变更/销户
    @Override
    public RetAtsRecUpdate maintenanceSubAccountInfo(ReqUpdateSubAccount p_subAccount) throws Exception {
        if ("0".equals(p_subAccount.operType)) {
            //新增申请
            return b2e0549(p_subAccount);
        } else if ("1".equals(p_subAccount.operType)) {
            return b2e0551(p_subAccount);
        } else {
            return b2e0552(p_subAccount);
        }
    }

    //出口商申请
    public RetAtsRecUpdate b2e0549(ReqUpdateSubAccount p_subAccount) {
        RetAtsRecUpdate retAts = new RetAtsRecUpdate(p_subAccount.reqHeader);
        retAts.writeStart();
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0549";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0549-rq");
            xmlWriter.writeStartElement("b2e0549-rq");
            xmlWriter.writeElementString("trxseqno", bankHeader.trnid);//报文流水号
            xmlWriter.writeElementString("reqtime", Format.DateTime14Format.format(new Date()));//请求时间yyyymmddhhmmss，长度 14
            xmlWriter.writeElementString("agentcode", p_subAccount.agentCode);//母公司编码
            xmlWriter.writeElementString("subagentcode", p_subAccount.subAccount);//子公司编码
            xmlWriter.writeElementString("companycode", p_subAccount.companyCode);//统一使用社会信用代码证，企业的唯一标识，变更视为新的企业
            xmlWriter.writeElementString("exportername", p_subAccount.customerName);//出口商名称（中文）
            xmlWriter.writeElementString("engname", p_subAccount.customerNameEn);//出口商名称（英文）
            xmlWriter.writeElementString("exporteradd", "");//企业地址（中文）
            xmlWriter.writeEndDocument();
            //是否需要和前一次请求保持5秒的间隔
            boolean isNeedWait = isNeedWaitFiveS();
            if (isNeedWait) {
                if (bankFront.lastVisitTime != null) {
                    long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
                    if (timeSpan < 5000l) {
                        long sleepMils = 5000l - timeSpan;
                        this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                        Thread.sleep(5000l - timeSpan);
                    }
                }
            }

            String tmpRetStr[] = sendToBank(xmlWriter.toXmlString());

            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        String tmpEachRspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/status/rspcod");
                        String tmpEachRspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/status/rspmsg");
                        if ("B001".equals(tmpEachRspcod)) {
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_subAccount.reqHeader.reqSeqID;
                            //报文处理状态， S成功，F失败,U未明
                            String handleStats = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/status");
                            if ("S".equals(handleStats)) {
                                //0000-申请成功； 0001-申请未明；其他返回错误码见银行文档附录
                                String resultCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/retcode");
                                //返回信息
                                String resultMsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/retmsg");
                                if ("0000".equals(resultCode)) {
                                    retAts.transState = "1";
                                    retAts.exporterCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0549-rs/b2e0549-rs/exportercode");
                                } else if ("0001".equals(resultCode)) {
                                    retAts.transState = "3";
                                } else {
                                    retAts.transState = "2";
                                }
                                retAts.payInfoCode = resultCode;
                                retAts.respInfo = resultMsg;
                            } else {
                                retAts.transState = "3";
                                retAts.payInfoCode = handleStats;
                                retAts.respInfo = tmpEachRspmsg;
                            }
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpEachRspmsg;
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        }
                    } else {
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retAts.respCode = "-1";
                        retAts.respInfo = tmprspmsg;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpRetStr[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = "出现异常，详情查看DSP日志";
            WriteBankLogLn("出现异常：" + e.getMessage());
            e.printStackTrace();
        }
        retAts.writeXml(retAts);
        return retAts;
    }

    //子账户变更
    public RetAtsRecUpdate b2e0551(ReqUpdateSubAccount p_subAccount) {
        RetAtsRecUpdate retAts = new RetAtsRecUpdate(p_subAccount.reqHeader);
        retAts.writeStart();
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0551";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0551-rq");
            xmlWriter.writeStartElement("b2e0551-rq");
            xmlWriter.writeElementString("trxseqno", bankHeader.trnid);//报文流水号
            xmlWriter.writeElementString("reqtime", Format.DateTime14Format.format(new Date()));//请求时间yyyymmddhhmmss，长度 14
            xmlWriter.writeElementString("agentcode", p_subAccount.agentCode);//母公司编码
            xmlWriter.writeElementString("exportercode", p_subAccount.subAccount);//出口商编码
            xmlWriter.writeElementString("companycode", p_subAccount.companyCode);//统一使用社会信用代码证，企业的唯一标识，变更视为新的企业
            xmlWriter.writeEndDocument();
            //是否需要和前一次请求保持5秒的间隔
            boolean isNeedWait = isNeedWaitFiveS();
            if (isNeedWait) {
                if (bankFront.lastVisitTime != null) {
                    long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
                    if (timeSpan < 5000l) {
                        long sleepMils = 5000l - timeSpan;
                        this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                        Thread.sleep(5000l - timeSpan);
                    }
                }
            }

            String tmpRetStr[] = sendToBank(xmlWriter.toXmlString());

            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        String tmpEachRspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/status/rspcod");
                        String tmpEachRspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/status/rspmsg");
                        if ("B001".equals(tmpEachRspcod)) {
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_subAccount.reqHeader.reqSeqID;
                            //报文处理状态S成功，F失败；
                            String handleStats = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/status");
                            if ("S".equals(handleStats)) {
                                //0000-变更成功；变更失败返回码见附录
                                String resultCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/retcode");
                                //返回信息
                                String resultMsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/retmsg");
                                if ("0000".equals(resultCode)) {
                                    retAts.transState = "1";
                                    retAts.exporterCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0551-rs/b2e0551-rs/exportercode");
                                } else if ("0001".equals(resultCode)) {
                                    retAts.transState = "3";
                                } else {
                                    retAts.transState = "2";
                                }
                                retAts.payInfoCode = resultCode;
                                retAts.respInfo = resultMsg;
                            } else {
                                retAts.transState = "3";
                                retAts.payInfoCode = handleStats;
                                retAts.respInfo = tmpEachRspmsg;
                            }
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpEachRspmsg;
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        }
                    } else {
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retAts.respCode = "-1";
                        retAts.respInfo = tmprspmsg;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpRetStr[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = "出现异常，详情查看DSP日志";
            WriteBankLogLn("出现异常：" + e.getMessage());
            e.printStackTrace();
        }
        retAts.writeXml(retAts);
        return retAts;
    }

    //出口商销户
    public RetAtsRecUpdate b2e0552(ReqUpdateSubAccount p_subAccount) {
        RetAtsRecUpdate retAts = new RetAtsRecUpdate(p_subAccount.reqHeader);
        retAts.writeStart();
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0552";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0552-rq");
            xmlWriter.writeStartElement("b2e0552-rq");
            xmlWriter.writeElementString("trxseqno", bankHeader.trnid);//报文流水号
            xmlWriter.writeElementString("reqtime", Format.DateTime14Format.format(new Date()));//请求时间yyyymmddhhmmss，长度 14
            xmlWriter.writeElementString("agentcode", p_subAccount.agentCode);//母公司编码
            xmlWriter.writeElementString("exportercode", p_subAccount.subAccount);//出口商编码
            xmlWriter.writeElementString("companycode", p_subAccount.companyCode);//统一使用社会信用代码证，企业的唯一标识，变更视为新的企业
            xmlWriter.writeEndDocument();
            //是否需要和前一次请求保持5秒的间隔
            boolean isNeedWait = isNeedWaitFiveS();
            if (isNeedWait) {
                if (bankFront.lastVisitTime != null) {
                    long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
                    if (timeSpan < 5000l) {
                        long sleepMils = 5000l - timeSpan;
                        this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                        Thread.sleep(5000l - timeSpan);
                    }
                }
            }

            String tmpRetStr[] = sendToBank(xmlWriter.toXmlString());

            if (tmpRetStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        String tmpEachRspcod = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/status/rspcod");
                        String tmpEachRspmsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/status/rspmsg");
                        if ("B001".equals(tmpEachRspcod)) {
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_subAccount.reqHeader.reqSeqID;
                            //报文处理状态S成功，F失败；
                            String handleStats = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/status");
                            if ("S".equals(handleStats)) {
                                //0000-销户成功；销户失败返回码见附录
                                String resultCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/retcode");
                                //返回信息
                                String resultMsg = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/retmsg");
                                if ("0000".equals(resultCode)) {
                                    retAts.transState = "1";
                                    retAts.exporterCode = tmpBkReader.getFirstTextNotNull("/bocb2e/trans/trn-b2e0552-rs/b2e0552-rs/exportercode");
                                } else if ("0001".equals(resultCode)) {
                                    retAts.transState = "3";
                                } else {
                                    retAts.transState = "2";
                                }
                                retAts.payInfoCode = resultCode;
                                retAts.respInfo = resultMsg;
                            } else {
                                retAts.transState = "3";
                                retAts.payInfoCode = handleStats;
                                retAts.respInfo = tmpEachRspmsg;
                            }
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpEachRspmsg;
                            if (StringHelper.isNullOrEmpty(retAts.respInfo)) {
                                retAts.respInfo = tmpEachRspcod;
                            }
                        }
                    } else {
                        if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                            tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                        }
                        retAts.respCode = "-1";
                        retAts.respInfo = tmprspmsg;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpRetStr[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = "出现异常，详情查看DSP日志";
            WriteBankLogLn("出现异常：" + e.getMessage());
            e.printStackTrace();
        }
        retAts.writeXml(retAts);
        return retAts;
    }
}
